package com.project.service.Imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.bean.ClassBean;
import com.project.bean.RoomBean;
import com.project.mapper.IRoomMapper;
import com.project.service.IRoomService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;

public class RoomService extends BaseService implements IRoomService {
    @Override
    public List<RoomBean> findSexRoom(String sex) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        List<RoomBean> sexRoom = mapper.findSexRoom(sex);
        session.close();
        return sexRoom;
    }

    @Override
    public PageInfo<RoomBean> findRooms(String address, String status, String canLive, String type, Integer pageNum) {
        PageHelper.startPage(pageNum,2);
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        PageInfo<RoomBean> rooms = PageInfo.of(mapper.findRooms(address, status, canLive, type, pageNum));
        session.close();
        return rooms;
    }

    @Override
    public void addRoom(RoomBean roomBean) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        mapper.addRoom(roomBean);
        session.commit();
        session.close();
    }

    @Override
    public RoomBean findRoomById(Integer roomId) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        RoomBean roomById = mapper.findRoomById(roomId);
        session.close();
        return roomById;
    }

    @Override
    public boolean deleteRoom(Integer roomId) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        Integer man = mapper.countMan(roomId);
        if (man==0){
            mapper.deleteRoom(roomId);
            session.commit();
            session.close();
            return true;
        }
        return false;
    }
}
